package sharemem

import (
	"server_cluster/common/protoc/pb"
	"sync/atomic"
)

type EquipJewel struct {
	Id       MyInt32 // 装备宝石id
	Cell     int32   // 格子
	EquipKey int32   // 镶嵌装备 在线数据
}

func (j *EquipJewel) MemInit() {
	j.Id.NewVal = 0
	j.Id.OldVal = 0
	j.Id.Status = 0
	j.Cell = 0
	j.EquipKey = 0
}

func (j *EquipJewel) Clear() {
	j.Id.NewVal = 0
}

func (j *EquipJewel) IsUse() bool {
	return j.Id.NewVal != 0 || j.Id.OldVal != 0
}

func (j *EquipJewel) GetId() int32 {
	return j.Id.NewVal
}

func (j *EquipJewel) SetSync() {
	atomic.AddUint32(&(j.Id.Status), 1)
}

func BuildEquipJewelKey(id int32, cell int32) int32       { return id*1000 + cell }
func ParseEquipJewelKey(key int32) (id int32, cell int32) { return key / 1000, key % 1000 }

func (u *User) RemoveEquipJewel(equipJewelKey, cell int32) {
	u.DeleteEquipJewelMap(equipJewelKey)
	u.EquipJewel[cell].Clear()
}

func (u *User) GetEquipJewelRemainCellNum() int32 {
	return int32(USER_EQUIP_JEWEL - len(u.GetEquipJewelMap()))
}

func PacketEquipJewel(equipJewel *EquipJewel) *pb.EquipJewel {
	return &pb.EquipJewel{
		EquipJewelKey: BuildEquipJewelKey(equipJewel.GetId(), equipJewel.Cell),
		EquipKey:      equipJewel.EquipKey,
		ItemId:        equipJewel.GetId(),
	}
}
